﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using DataTier;

namespace DataTier
{
    public class SapConnectionCtr
    {
        private static SapConnectionCtr instance = null;
        private SapConfig sapConfig = SapConfig.Instance;
        public static SapConnectionCtr Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SapConnectionCtr();
                }
                return instance;
            }
        }

        public IRfcTable GetAllExpenses(DateTime startDate, DateTime endDate, string reviewed, string prePaid, string paid, string toAmount, string fromAmount, string empNr, List<string> types)
        {
            RfcDestinationManager.RegisterDestinationConfiguration(sapConfig);

            RfcDestination sap = RfcDestinationManager.GetDestination("DEV");

            //Set Function
        
                IRfcFunction function = sap.Repository.CreateFunction("ZGET_PERSONAL_EXPENSES");

                IRfcStructure dexpsStructure = function.GetStructure("DEXPS");

                dexpsStructure.SetValue("SIGN", "I");
                dexpsStructure.SetValue("OPTION", "BT");
                dexpsStructure.SetValue("LOW", startDate);
                dexpsStructure.SetValue("HIGH", endDate);

                function.SetValue("DEXPS", dexpsStructure);

                if (!reviewed.Equals("Alle"))
                {
                    IRfcStructure reviewedStructure = function.GetStructure("REVIEWED");

                    reviewedStructure.SetValue("SIGN", "I");
                    reviewedStructure.SetValue("OPTION", "EQ");
                    reviewedStructure.SetValue("LOW", reviewed);

                    function.SetValue("REVIEWED", reviewedStructure);
                }

                if (!prePaid.Equals("Alle"))
                {
                    IRfcStructure prevPaidStructure = function.GetStructure("PREV_PAID");

                    prevPaidStructure.SetValue("SIGN", "I");
                    prevPaidStructure.SetValue("OPTION", "EQ");
                    prevPaidStructure.SetValue("LOW", prePaid);

                    function.SetValue("PREV_PAID", prevPaidStructure);
                }

                if (!paid.Equals("Alle"))
                {
                    IRfcStructure paidStructure = function.GetStructure("PAID");

                    paidStructure.SetValue("SIGN", "I");
                    paidStructure.SetValue("OPTION", "EQ");
                    paidStructure.SetValue("LOW", paid);

                    function.SetValue("PAID", paidStructure);
                }

                if (fromAmount.Length > 0)
                {
                    IRfcStructure amountStructure = function.GetStructure("AMNT");

                    amountStructure.SetValue("SIGN", "I");
                    amountStructure.SetValue("OPTION", "BT");
                    amountStructure.SetValue("LOW", fromAmount);
                    amountStructure.SetValue("HIGH", toAmount);

                    function.SetValue("AMNT", amountStructure);
                }
                if (empNr.Length > 0)
                {
                    function.SetValue("EMPID", empNr);
                }

                if (types.Count > 0)
                {
                    IRfcTable typeTable = function.GetTable("EXPTYPE_ID");

                    foreach (string s in types)
                    {
                        IRfcStructure structure = typeTable.Metadata.LineType.CreateStructure();

                        structure.SetValue("SIGN", "I");
                        structure.SetValue("OPTION", "EQ");
                        structure.SetValue("LOW", s);

                        typeTable.Insert(structure);
                    }
                    function.SetValue("EXPTYPE_ID", typeTable);

                    
                }



                function.Invoke(sap);

                //Get reference to table object

                return function.GetTable("E_PSPNR");
            
        }

        public IRfcTable GetTableStructure()
        {
            RfcDestinationManager.RegisterDestinationConfiguration(sapConfig);

            RfcDestination sap = RfcDestinationManager.GetDestination("DEV");

            //Set Function
            IRfcFunction function = sap.Repository.CreateFunction("ZGET_PERSONAL_EXPENSES");

            function.SetValue("OWN", "");

            return function.GetTable("E_PSPNR");

        }

        public void InsertEditedExpenses(IRfcTable irfcTable)
        {
            RfcDestinationManager.RegisterDestinationConfiguration(sapConfig);
            RfcDestination sap = RfcDestinationManager.GetDestination("DEV");

            //Set Function
            IRfcFunction function = sap.Repository.CreateFunction("ZSAVE_EXPENSES");

            //Inserts expenses to table "EXPENSES"
            function.SetValue("EXPENSES", irfcTable);
            function.Invoke(sap);

            ////FEJL struktur BAPIRET2
            IRfcTable fejlTabel = function.GetTable("return");

            string fejl = fejlTabel.GetString("MESSAGE");

        }


        /*
         * Get Employees
         * Returning an irfcTable which contains a list of employees
         * Parameters first name or last name
         */
        public IRfcTable GetEmployees(string firstname, string lastname)
        {
            RfcDestinationManager.RegisterDestinationConfiguration(sapConfig);
            RfcDestination sap = RfcDestinationManager.GetDestination("DEV");

            IRfcFunction function = sap.Repository.CreateFunction("BAPI_EMPLOYEE_GETDATA");

            if (firstname.Length > 0)
            {
                function.SetValue("FSTNAME_M", firstname);
            }
            if (lastname.Length > 0)
            {
                function.SetValue("LASTNAME_M", lastname);
            }
            function.Invoke(sap);

            IRfcTable table = function.GetTable("PERSONAL_DATA");


            return table;
        }

    }
}
